﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Card.Common;
using System.Configuration;
using System.IO;

namespace Card.Web
{
    public class BasePage : Card.Security.Page
    {
        #region 属性
        protected virtual bool ValidateCity
        {
            get
            {
                return string.Compare(ConfigurationManager.AppSettings[CONST.AppSettings.ValidateArea], CONST.YES_NO.YES) == 0;
            }
        }

        public Card.Entity.Area _ParentArea = null;
        public Card.Entity.Area ParentArea
        {
            get
            {
                if (_ParentArea == null)
                {
                    if (this.Area != null)
                    {
                        _ParentArea = Card.Common.AreaCache.GetParentArea(Area);
                    }
                }
                return _ParentArea;
            }
        }

        public Card.Entity.Area _Area = null;
        public Card.Entity.Area Area
        {
            get
            {
                return _Area;
            }
        }

        public string SiteName
        {
            get
            {
                return Card.Common.CardWebUtility.GetSiteName();
            }
        }

        public string SiteDomain
        {
            get
            {
                return Card.Common.CardWebUtility.GetSiteDomain().TrimEnd('/');
            }
        }

        private string _Theme = string.Empty;
        public string Theme
        {
            get
            {
                if (string.IsNullOrEmpty(_Theme))
                {
                    if (Directory.Exists(this.MapPath(string.Format(@"Themes/{0}/", this.Area.SiteCode))))
                    {
                        _Theme = this.Area.SiteCode;
                    }
                    else
                    {  
                        _Theme = "default";
                    }
                }
                return _Theme;
            }
        }

        public string AreaDesc
        {
            get
            {
                return this.Area == null ? "" : (this.ParentArea != null ? string.Format("{0}", this.ParentArea.Name) : string.Empty) + this.Area.Name;
            }
        }
        #endregion

        #region override
        /// <summary>
        /// 初始化身份信息
        /// </summary>
        protected override void InitPassport()
        {
            base.InitPassport();
            this.AutoLogin();
        }

        protected virtual void InitArea()
        {
            _Area = Card.Common.AreaCache.GetAreaByCode(CardWebUtility.GetAreaCode());
            if ((_Area == null || string.Compare(_Area.IsActive, CONST.YES_NO.YES) != 0) && ValidateCity)
            {
                _Area = Card.Common.AreaCache.GetAreaByCode(Card.Security.CookieManager.GetArea());
                if (_Area == null || string.Compare(_Area.IsActive, CONST.YES_NO.YES) != 0)
                {
                    Response.Redirect(string.Format("{0}/changecity.aspx?Target={1}", SiteDomain, this.Request.Url.AbsolutePath));
                }
                else
                {
                    Response.Redirect(string.Format("{0}{1}", _Area.SiteUrl.TrimEnd('/'), this.Request.Url.AbsolutePath));
                }
            }
            else if (_Area == null || string.Compare(_Area.IsActive, CONST.YES_NO.YES) != 0)
            {
                _Area = Card.Common.AreaCache.GetAreaByCode(Card.Security.CookieManager.GetArea());
                if (_Area == null || string.Compare(_Area.IsActive, CONST.YES_NO.YES) != 0)
                {
                    _Area = Card.Common.AreaCache.GetAreaByCode(CONST.MainArea);
                }
            }
            if (_Area != null)
            {
                Card.Security.CookieManager.SetArea(_Area.SiteCode);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitArea();
            base.OnInit(e);
        }

        #endregion

        #region 方法
        /// <summary>
        /// 自动登录
        /// </summary>
        private void AutoLogin()
        {
            if (!this.Passport.IsLogin)
            {
                Common.LoginUtility.AutoLogin(this.Passport);
            }
        }

        /// <summary>
        /// 注册脚本
        /// </summary>
        /// <param name="strScript"></param>
        /// <param name="addScriptTags"></param>
        protected void RegisterScript(string strScript, bool addScriptTags)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), strScript, addScriptTags);
        }
        /// <summary>
        /// 弹出alert信息
        /// </summary>
        /// <param name="message"></param>
        public void MessageBox(string message)
        {
            string strScript = string.Format("alert('{0}');", message);
            this.RegisterScript(strScript, true);
        }

        /// <summary>
        /// 弹出alert信息，然后跳转
        /// </summary>
        /// <param name="message"></param>
        /// <param name="redirectUrl"></param>
        public void MessageBox(string message, string redirectUrl)
        {
            string strScript = string.Format("alert('{0}');window.location.href='" + redirectUrl + "';", message);
            this.RegisterScript(strScript, true);
        }
        #endregion
    }
}
